/*
 * Copyright (C) 2010 第2回 SHARPハッカソン＠東広島
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package android.hackathon.bp.hayaben;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.Toast;

import com.felicanetworks.mfc.AppInfo;
import com.felicanetworks.mfc.FelicaEventListener;

/**
 * FelicaでPushされてきたデータを処理する。
 *
 * @author @miyatay, Felica API
 * @author @esmasui, Bluetooth API
 * @author @mhidaka, FelicaHackathon@osaka
 */
public class ReceiverActivity extends Activity {

	static final String TAG = "ReceiverActivity";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.receiver);
	}

	@Override
	protected void onStart() {
		super.onStart();

		doReceive();
	}

	@Override
	protected void onResume() {
		super.onResume();
		FelicaService service = FelicaService.getInstance();
		service.setContext(this, new FelicaEventListener() {
			public void finished() {
			}

			public void errorOccurred(int arg0, String arg1, AppInfo arg2) {
				// String msg;
				// msg = String.format(
				// "FelicaEventListener#errorOccurred(%s,%s,%s)", arg0,
				// arg1, arg2);
				// m(msg);
			}
		});

		try {
			service.connectAndActivate();
		} catch (Exception e) {
			// e.printStackTrace();
			// String msg = e.getMessage();
			// m(msg);
		}
	}

	@Override
	protected void onPause() {
		super.onPause();

		FelicaService service = FelicaService.getInstance();
		try {
			service.disconnectAndInactivate();
		} catch (Exception e) {
			// Log.e(TAG, e.getMessage(), e);
			// String msg = e.toString();
			// m(msg);
		}
	}


	void doReceive() {
		//データ受信
		String data = getIntent().getDataString();
		m(data);

		//TODO: データ処理

		//finish();
	}

	void m(String text) {
		Toast t = Toast.makeText(this, text, Toast.LENGTH_SHORT);
		t.show();
	}

}